package com.hy.hongying.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hy.hongying.bo.VideoAddBO;
import com.hy.hongying.bo.VideoCallAdmireBo;
import com.hy.hongying.bo.VideoMyselfBo;
import com.hy.hongying.dao.VideoDao;
import com.hy.hongying.entity.Coins;
import com.hy.hongying.entity.Video;
import com.hy.hongying.service.intf.VideoService;
import com.hy.hongying.third.BaiduCensorUtil;
import com.hy.hongying.util.BeanCopy;
import com.hy.hongying.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoDao dao;
    @Override
    public R videoInsert(String token,VideoAddBO bo) {
        if (token==null||token.length()==0){
            return R.fail("亲这里需要登录哦");
        }
        if (bo!=null){
            //先文本检测是否违法
            boolean b = BaiduCensorUtil.textCensor(bo.getInfo(), bo.getTitle());
            if (b){
                Video video = BeanCopy.getTrance(Video.class, bo);
                video.setCtime(new Date());
                video.setFlag(1);
                dao.insert(video);
                return R.ok("上传成功");
            }else {
                //后期判断如果1个小时发送3条违法言论，就不能发布视屏
                return R.fail("亲请注意你的发言哦");
            }
        }
        return R.fail("数据异常，请检查数据是否正确");
    }

    @Override
    public R videoMyself(String token,Integer uid) {
        if (token==null||token.length()==0){
            return R.fail("亲这里需要登录哦");
        }
        if (uid>0){
            List<VideoMyselfBo> videoMyselfBos = dao.myVideo(uid);
            if (videoMyselfBos!=null&&videoMyselfBos.size()>0){
                return R.ok(videoMyselfBos);
            }
            return R.ok("亲你还没有发布动态哦");
        }
        return R.fail("网络异常，请稍后再试");
    }

    @Override
    public R watchVideo(Integer type) {
        if (type!=null&&type>0){
            System.err.println(type);
            List<VideoMyselfBo> videoMyselfBos = dao.watchVideoType(type);
            if (videoMyselfBos!=null&&videoMyselfBos.size()>0){
                return R.ok(videoMyselfBos);
            }else {
                return R.fail("网络异常,请稍后");
            }
        }else {
            List<VideoMyselfBo> videoMyselfBos = dao.watchVideoNoType();
            if (videoMyselfBos!=null&&videoMyselfBos.size()>0){
                return R.ok(videoMyselfBos);
            }else {
                return R.fail("网络异常,请稍后");
            }

        }
    }

    @Override
    public R videoDel(Integer oid) {
        if (oid==0){
            return R.fail("数据异常,请稍后");
        }
        if (dao.update(oid)>0){
            return R.ok("删除成功");
        }

        return R.fail("网络异常");
    }
/*
用户查找视频
 */
    @Override
    public R VideoLike(String title) {
        if (title!=null&&title.length()>0){
            System.err.println(title);
          return  R.ok(dao.watchVideoTypes(title));
        }
        return R.ok(dao.watchVideoNoType());
    }

    /*
    视频投币
     */
    @Transactional
    @Override
    public R videoCallAdmire(String token, VideoCallAdmireBo bo) {
        if (token==null||token.length()==0){
            return R.fail("亲这里需要登录哦");
        }
        if (bo==null){
            return R.fail("数据异常。。取消本次交易余额不足！！！");
        }
        /*
        先判断用户的金钱是否足够
         */
        int i = dao.selectUserIntegral(bo.getUid());
        if (i<bo.getCount()){
            return R.fail("余额不足！！！");
        }
        /*
        操作者减除币
         */
        dao.updateUserIntegral01(bo.getCount(), bo.getUid());
        /*
        作者查询 币添加
         */
        QueryWrapper<Video> coinsQueryWrapper = new QueryWrapper<>();
        coinsQueryWrapper.eq("id",bo.getVid());
        List<Video> videos = dao.selectList(coinsQueryWrapper);
        int oid=videos.get(0).getOid();
        dao.updateUserIntegral02(bo.getCount(), videos.get(0).getUid());
        /*
        金币交易完成，添加历史记录,返回总数
         */
        Coins coins = new Coins(oid,bo.getCount(),bo.getUid());
        dao.insertCoins(coins);
        Integer  num= dao.selectAllOssCoins(oid);
        return R.ok(num);
    }

}
